import { Dictionary } from "../../../Support/Share/Lib/Dictionary";
import { ISingleton, Singleton } from "../../Singleton/Singleton"
import { EventSystem } from "../EventSystem/EventSystem";
import { IMerge } from "./IMerge";

export module ConfigComponentInvoke {

    export class GetAllConfig {

    }

}

export class ConfigComponent extends Singleton<ConfigComponent>{

    private static _Instance: ConfigComponent;
    static get Instance() {
        return this._Instance;
    }

    protected set Instance(value: ConfigComponent) {
        ConfigComponent._Instance = value;
    }

    private readonly allConfig = new Dictionary<string, ISingleton>();

    public override Dispose() {
        for (const key in this.allConfig) {
            const Value = this.allConfig.TryGetValue(key);
            Value.Destroy();
        }
    }

    public LoadOneConfig(configType: string) {

    }

    public Load() {

    }

    public async LoadAsync() {
        this.allConfig.Clear();
        const configBytes = EventSystem.Instance.Invoke<ConfigComponentInvoke.GetAllConfig, Dictionary<number, any>>(new ConfigComponentInvoke.GetAllConfig());
        for (const key in configBytes) {
            const oneConfigBytes = configBytes[key];
            const singleton = oneConfigBytes as ISingleton;
            singleton.Register();
        }
        for (const key in configBytes) {
            const oneConfigBytes = configBytes[key];
            const category = oneConfigBytes as IMerge;
            category.ProtoEndInit();
        }
    }

}